package com.gxuwz.attend.dao.admin;

import com.gxuwz.attend.entity.Instructor; // 假设你有一个 Instructor 实体类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Repository
public class InstructorDao {
    @Autowired

    private JdbcTemplate jdbcTemplate;


    // 获取所有辅导员
    public List<Instructor> getAllInstructors(int size, int offset) {
        String sql = "SELECT * FROM sys_instructor LIMIT ? OFFSET ?";
        return jdbcTemplate.query(sql, new Object[]{size, offset}, (rs, rowNum) -> {
            Instructor instructor = new Instructor();
            instructor.setInstID(rs.getString("instID"));
            instructor.setInstName(rs.getString("instName"));
            instructor.setDepID(rs.getString("depID"));
            instructor.setTelephone(rs.getString("telephone"));
            instructor.setPassword(rs.getString("password")); // 假设数据库中也有密码字段
            return instructor;
        });
    }

    // 添加辅导员
    public void addInstructor(Instructor instructor) {
        String sql = "INSERT INTO sys_instructor(instID, instName, depID, telephone, password) VALUES(?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql, instructor.getInstID(), instructor.getInstName(), instructor.getDepID(), instructor.getTelephone(), instructor.getPassword());
    }

    // 删除辅导员
    public boolean deleteInstructor(String instID) {
        String sql = "DELETE FROM sys_instructor WHERE instID=?";
        int rs = jdbcTemplate.update(sql, instID);
        return rs > 0; // 返回是否删除成功
    }

    // 更新辅导员信息
    public boolean updateInstructor(Instructor instructor) {
        String sql = "UPDATE sys_instructor SET instID=? ,instName=?, depID=?, telephone=?, password=? WHERE instID=?";
        int rs = jdbcTemplate.update(sql, instructor.getInstID(),instructor.getInstName(), instructor.getDepID(), instructor.getTelephone(), instructor.getPassword(),instructor.getId() );
        return rs > 0; // 返回是否更新成功
    }

    // 根据条件查找辅导员
    public List<Instructor> findInstructors(String instID, String instName, int size, int offset) {
        // 构建基本的 SQL 查询
        StringBuilder sql = new StringBuilder("SELECT * FROM sys_instructor WHERE 1=1");
        List<Object> params = new ArrayList<>();

        // 根据工号模糊查询
        if (instID != null && !instID.isEmpty()) {
            sql.append(" AND instID LIKE ?");
            params.add("%" + instID + "%");
        }

        // 根据姓名模糊查询
        if (instName != null && !instName.isEmpty()) {
            sql.append(" AND instName LIKE ?");
            params.add("%" + instName + "%");
        }

        // 添加分页限制
        sql.append(" LIMIT ? OFFSET ?");
        params.add(size);
        params.add(offset);

        // 执行查询并返回结果
        return jdbcTemplate.query(sql.toString(), params.toArray(), (rs, rowNum) -> {
            Instructor instructor = new Instructor();
            instructor.setInstID(rs.getString("instID"));
            instructor.setInstName(rs.getString("instName"));
            instructor.setDepID(rs.getString("depID"));
            instructor.setTelephone(rs.getString("telephone"));
            instructor.setPassword(rs.getString("password")); // 假设数据库中也有密码字段
            return instructor;
        });
    }
}
